#!/usr/bin/env php
<?php

if (!isset($argv[1])) {
    echo "Missing argument.\n";
    echo "Usage: break-long-lines <filename>\n";
    exit(1);
}

$filename = $argv[1];
$maxLength = 80;

// Read the entire file at once and then split into lines
$data = file_get_contents($filename);
$lines = explode("\n", $data);

$data = '';

// If any line is greater than the max length, split it up
foreach ($lines as $line) {
    if (strlen($line) > $maxLength) {
        $line = chunk_split($line, $maxLength, "\n");
    }
    $data .= $line . "\n";
}

// Write the contents back to the file
file_put_contents($filename, $data);
exit(0);
